#include "addexpenddialog.h"
#include "ui_addexpenddialog.h"
#include "mainwindow.h"
#include "colorbutton.h"
#include <QLinkedList>
#include <QDebug>

AddExpendDialog::AddExpendDialog(QMainWindow *parent) :
    QDialog(parent),
    ui(new Ui::AddExpendDialog)
{
    ui->setupUi(this);
    Initialize();
}

AddExpendDialog::~AddExpendDialog()
{
    delete ui;
}

void AddExpendDialog::Initialize()
{
    //database.openConnection();
    btn_group = new QButtonGroup();

    this->mNewExpDlg = NULL;
    loadExpenseItemsList();
}

void AddExpendDialog::on_pushButtonAddExpenses_clicked()
{
    NewExpenseDialog *mNewExpDlg = this->getMNewExpDlg();
    if (!mNewExpDlg)
    {
        mNewExpDlg = new NewExpenseDialog(this);
    }
    mNewExpDlg->setModal(true);
    mNewExpDlg->show();
    mNewExpDlg->raise();
    mNewExpDlg->activateWindow();

}
QButtonGroup *AddExpendDialog::getBtn_group() const
{
    return btn_group;
}

void AddExpendDialog::setBtn_group(QButtonGroup *value)
{
    btn_group = value;
}

NewExpenseDialog *AddExpendDialog::getMNewExpDlg() const
{
    return mNewExpDlg;
}

void AddExpendDialog::setMNewExpDlg(NewExpenseDialog *value)
{
    mNewExpDlg = value;
}


void AddExpendDialog::loadExpenseItemsList()
{
    QLinkedList<ExpenseItems*> *exp_item_list = database.selectExpenseItems();
    ExpenseItems* exp_item;
    int i = 0;
    int x = 0;
    int y = 0;

    int y_const = 90;
    QString color_str;
    QString obj_name;
    ColorButton *color_btn;
    y = (exp_item_list->size() / 8) * 70 + y_const;
    x = (exp_item_list->size() % 8) * 70;
    this->ui->pushButtonAddExpenses->setGeometry(QRect(x, y, 64, 64));

    x = 0;
    y = 0;
    while(!exp_item_list->isEmpty())
    {
        exp_item = exp_item_list->takeFirst();
        y = (i / 8) * 70 + y_const;
        x = (i % 8) * 70;
        color_str = exp_item->getColor();
        obj_name = "color_btn_" + QString::number(i);
        color_btn = new ColorButton(this);
        color_btn->setObjectName(obj_name);
        color_btn->setGeometry(QRect(x, y, 64, 64));
        color_btn->setAutoFillBackground(true);
        qDebug() << "add exp:" << color_str;
        color_btn->setStyleSheet("background-color: #" + color_str + "; color: rgb(255, 255, 255)");
        color_btn->setColor(exp_item->getId());
        color_btn->show();
        btn_group->addButton(color_btn, exp_item->getId());

        i++;
    }

}




